package cn.yujian95.hospital.service.impl;

import cn.yujian95.hospital.dto.param.HospitalCmsParam;
import cn.yujian95.hospital.entity.HospitalCms;
import cn.yujian95.hospital.mapper.HospitalCmsMapper;
import cn.yujian95.hospital.service.HospitalCmsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class HospitalCmsServiceImpl extends ServiceImpl<HospitalCmsMapper, HospitalCms> implements HospitalCmsService {

    /**
     * 添加轮播图信息
     *
     * @param param
     * @return
     */
    @Override
    public boolean insert(HospitalCmsParam param) {
        HospitalCms hospitalCms = new HospitalCms();
        BeanUtils.copyProperties(param, hospitalCms);
        return save(hospitalCms);
    }

    /**
     * 轮播图是否存在
     *
     * @param id
     * @return
     */
    @Override
    public boolean countCms(Long id) {
        HospitalCms hospitalCms = getById(id);
        return hospitalCms != null;
    }

    /**
     * 获取轮播图信息
     *
     * @param id
     * @return
     */
    @Override
    public HospitalCms getHospitalCmsInfo(Long id) {
        HospitalCms hospitalCms = getById(id);

        return hospitalCms;
    }

    /**
     * 更新轮播图信息
     *
     * @param id
     * @param param
     * @return
     */
    @Override
    public boolean updateCms(Long id, HospitalCmsParam param) {
        HospitalCms hospitalCms = new HospitalCms();
        BeanUtils.copyProperties(param, hospitalCms);
        hospitalCms.setId(id);
        return updateById(hospitalCms);
    }

    /**
     * 删除轮播图信息
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteCms(Long id) {
        return removeById(id);
    }

    /**
     * 分页获取轮播图信息
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<HospitalCms> listWithQuery(String name, Integer pageNum, Integer pageSize) {
        IPage<HospitalCms> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<HospitalCms> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isEmpty(name)) {
            lambdaQueryWrapper.like(HospitalCms::getName, name);
        }
        lambdaQueryWrapper.orderByAsc(HospitalCms::getSort);
        IPage<HospitalCms> page1 = page(page, lambdaQueryWrapper);

        return page1;
    }

    @Override
    public List<HospitalCms> searchHospitalCmsInfoList() {
        List<HospitalCms> list = list();
        return list;
    }
}
